const express = require("express");

const fs = require("fs");

var morgan = require("morgan"); //日志
var path = require("path");
var FileStreamRotator = require("file-stream-rotator"); // 日志切割
var proxyMiddleware = require("http-proxy-middleware");
const bodyParser = require("body-parser");

// const favicon = require('serve-favicon')

const history = require("connect-history-api-fallback");
const app = express();
const port = 7882;
// var JHConfig = {
// 	action_url: 'http://192.168.28.248:8088/rechargeCardServer/',
// 	'domain': '*'
// }
// 日志 start
var logDirectory = path.join(__dirname, "log");

// ensure log directory exists
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory);

// create a rotating write stream
var accessLogStream = FileStreamRotator.getStream({
    date_format: "YYYYMMDD",
    filename: path.join(logDirectory, "access-%DATE%.log"),
    frequency: "daily",
    verbose: false
});

// setup the logger
// app.use(morgan('combined', {stream: accessLogStream}))
// 日志 end

//代理路径//自营借口,//自营借口
var context = ["/rechargeCardServer", "/dataWay"];

var proxypath = "http://localhost:80/";
var options = {
    target: proxypath,
    changeOrigin: true
};
if (context.length) {
    app.use(proxyMiddleware(context, options));
}

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// app.use(favicon(path.resolve(__dirname , 'manage/static/img/favicon.ico')))
app.use(express.static("./manage"));

app.use(
    history({
        // htmlAcceptHeaders: ['text/html', 'application/xhtml+xml']
    })
);

app.listen(port, () => {
    console.log(`listening on port http://localhost:${port}`);
});

module.exports = app;
